//


#import "NT_LeftView.h"
#import "NT_LeftTableViewCell.h"
#import "NT_LeftHeaderView.h"
#import "NT_FootAddView.h"


//
@interface NT_LeftView ()<UITableViewDelegate, UITableViewDataSource>
/** <#Description#>*/
@property (nonatomic, strong) UITableView *tableView;
/** <#Description#>*/
@property (nonatomic, strong) NSArray *oneArray;
/** <#Description#>*/
@property (nonatomic, strong) NSArray *oneImgArray;
/** <#Description#>*/
@property (nonatomic, strong) NSMutableArray *twoArray;
/** <#Description#>*/
@property (nonatomic, strong) NSMutableArray *twoImgArray;
/** <#Description#>*/
@property (nonatomic, strong) NT_CoreDataBase *data;
/** <#Description#>*/
@property (nonatomic, strong) NSMutableArray *dataArray;


@end

@implementation NT_LeftView

- (instancetype)initWithFrame:(CGRect)frame {
    if ([super initWithFrame:frame]) {
        _data = [[NT_CoreDataBase alloc]initWithCoreData:@"NoteCategory" modelName:@"NoteData" success:^{} fail:^(NSError * _Nullable error) { }];
        //
        if (![[NSUserDefaults standardUserDefaults]objectForKey:@"isFirst" ]) {
            [self savedata];
        }
        [self addSubview:self.tableView];
        [self getCategoryData];
    }return self;
}


//#pragma mark ----------------- tableViewDelegate ----------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 2;
    }else if (section == 1){
        return _dataArray.count;
    }
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cellIdentifier";
    NT_LeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[NT_LeftTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if (indexPath.section == 0) {
        cell.img.image = [UIImage imageNamed:self.oneImgArray[indexPath.row]];
        cell.titleLabel.text = self.oneArray[indexPath.row];
    }else if (indexPath.section == 1){
        //
        NoteCategory *model = self.dataArray[indexPath.row];
        cell.titleLabel.text = model.title;
        cell.img.image = (UIImage*)model.picture;
        
    }
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section != 2) {
        return kRelativeHeight(50);
    }else
        return 8;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    NT_LeftHeaderView *vc = [[NT_LeftHeaderView alloc]init];
    vc.backgroundColor = RGB(245, 245, 245);
    if (section == 0) {
        vc.titleLabel.text = @"Note";
        vc.editButton.hidden = YES;
    }else if (section == 1){
        vc.titleLabel.text = @"Note Categroy";
        vc.editButton.hidden = NO;
    }
    return vc;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 1) {
        return kRelativeHeight(40);
    }
    return 0.0001;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 1) {
        UIView *view = [[UIView alloc]init];
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 1)];
        line.backgroundColor = RGB(235, 235, 235);
        [view addSubview:line];
        UIButton *addBtn = [[UIButton alloc]initWithFrame:CGRectMake(kRelativeWidth(50), kRelativeHeight(10), kRelativeWidth(40), kRelativeHeight(20))];
        [addBtn setTitle:@"Add" forState:UIControlStateNormal];
        [addBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [addBtn addTarget:self action:@selector(newAdd) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:addBtn];
        return view;
    }
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        
        NSLog(@"%@",_oneArray[indexPath.row]);
        if (self.leftViewBlock) {
            self.leftViewBlock(indexPath,@"");
        }
    }else if (indexPath.section == 1){
        NoteCategory *model = self.dataArray[indexPath.row];
        NSLog(@"%@",_twoArray[indexPath.row]);
        if (self.leftViewBlock) {
            self.leftViewBlock(indexPath,model.title);
        }
    }else{
        NSLog(@"设置");
        if (self.leftViewBlock) {
            self.leftViewBlock(indexPath,@"");
        }
    }
    
}


//MARK:新建日记分类
- (void)newAdd {
    
    NT_FootAddView * v = [[NT_FootAddView alloc]init];
    [v showV];
    //
    __weak typeof(self) weakSelf = self;
    v.reloadBlock = ^{
        [weakSelf getCategoryData];
    };
    
}

//
#pragma mark ----------------- 懒加载 ----------------
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, UI_NAV_BAR_HEIGHT, kMainScreenWidth-kRelativeWidth(100), kMainScreenHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate   = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.rowHeight  = kRelativeHeight(50);
    }
    return _tableView;
}
- (NSArray *)oneArray {
    if (!_oneArray) {
        _oneArray = @[@"All Note",@"Lately Delete"];
    }return _oneArray;
}
- (NSArray *)oneImgArray {
    if (!_oneImgArray) {
        _oneImgArray = @[@"book9",@"delete9"];
    }return _oneImgArray;
}

- (NSMutableArray *)twoArray {
    if (!_twoArray) {
        _twoArray = [NSMutableArray arrayWithObjects:@{@"id":[self randomsssss],@"title":@"travel",@"picture":[UIImage imageNamed:@"travel3"]},@{@"id":[self randomsssss],@"title":@"invest",@"picture":[UIImage imageNamed:@"invest34"]},@{@"id":[self randomsssss],@"title":@"live",@"picture":[UIImage imageNamed:@"live54"]},@{@"id":[self randomsssss],@"title":@"job",@"picture":[UIImage imageNamed:@"work39"]}, nil];
    }
    return _twoArray;
}
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray new];
    }
    return _dataArray;
}

//
- (void)savedata {
    
    [[NSUserDefaults standardUserDefaults]setObject:@"1" forKey:@"isFirst"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    for (NSDictionary *dic in self.twoArray) {
        //
        [_data ntInsertNewEntity:dic success:^{
            
        } fail:^(NSError * _Nullable error) {
        }];
    }
    
}
//
- (void)getCategoryData {
    //
    [_data ntReadEntity:nil ascending:NO filterStr:nil success:^(NSArray * _Nonnull results) {
        if (!results) {
            return ;
        }
        //
        self.dataArray = [results copy];
        [self.tableView reloadData];
        
    } fail:^(NSError * _Nullable error) {
    }];
    
}
- (NSString *)randomsssss
{
    const int count =6;
    NSString * sourceString=@"126543789";
    NSMutableString *re=[[NSMutableString alloc]init];
    for (int i = 0; i < count; i++) {
        unsigned index=rand()%[sourceString length];
        NSString * s=[sourceString substringWithRange:NSMakeRange(index, 1)];
        [re appendString:s];
    }
    return re;
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
